package com.wdd.libbase.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.MainThread
import androidx.fragment.app.Fragment
import com.fengchen.uistatus.UiStatusController
import com.fengchen.uistatus.annotation.UiStatus
import com.wdd.libbase.annotation.BindEventBus
import com.wdd.libbase.mvp.IView
import com.wdd.libbase.view.HttpDialog
import leakcanary.AppWatcher
import org.greenrobot.eventbus.EventBus

/**
 *
 * @ProjectName:    OwnTool
 * @Package:        com.wdd.libbase.base
 * @ClassName:      BaseFragment
 * @Description:     最底层的fragment
 * @Author:         王丹丹
 * @CreateDate:     2019/12/10 17:27
 * @UpdateUser:     更新者
 * @UpdateDate:     2019/12/10 17:27
 * @UpdateRemark:   更新说明
 * @Version:        1.0
 */
abstract class BaseFragment : Fragment(), IView {
    //上下文
    protected lateinit var mContext: Context
    //fragment所关联的view
    protected lateinit var rootView: View
    //请求加载框
    private var httpDialog: HttpDialog? = null
    protected var mUiStatusController: UiStatusController? = null
    //默认false,如果要是懒加载，覆盖这个值为true
    open var isLazyLoad = false
    //是否加载数据（暂时用于第一次加载判断，以后也许会有其他情况）
    private var isNeedLoad = true

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        //判断是否需要注册EventBus
        if (this::class.java.isAnnotationPresent(BindEventBus::class.java) &&
            !EventBus.getDefault().isRegistered(this)
        ) {
            EventBus.getDefault().register(this)
        }
        rootView = LayoutInflater.from(context).inflate(getLayoutId(), container, false)
        return rootView
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
        if (!isLazyLoad)
            initData()

    }

    override fun onResume() {
        super.onResume()
        if (isNeedLoad && isLazyLoad) {
            initData()
            //数据已加载，置false，避免每次切换都重新加载数据
            isNeedLoad = false
        }
    }


    /**
     * 获取布局文件
     * @return
     */
    protected abstract fun getLayoutId(): Int

    /**
     * 进行界面绑定
     * @param view
     */
    open fun initView(rootView: View?) {
        mUiStatusController = UiStatusController.get().bind(rootView!!)
        mUiStatusController!!.changeUiStatus(UiStatus.CONTENT)
        mUiStatusController!!.setListener(
            UiStatus.EMPTY
        ) { _, _, _ ->
            retryLoad()
        }

        mUiStatusController!!.setListener(
            UiStatus.LOAD_ERROR
        ) { _, _, _ ->
            retryLoad()
        }
    }

    /**
     * 初始化数据
     */
    protected abstract fun initData()

    /**
     * 点击重试
     */
    @MainThread
    open fun retryLoad() {

    }

    /**
     * 展示加载弹窗
     */
    override fun showHttpLoading() {
        if (httpDialog == null) {
            httpDialog = HttpDialog(mContext)
        }
        if (!httpDialog!!.isShowing) {
            httpDialog!!.show()
        }
    }

    /**
     *隐藏弹窗
     */
    override fun hideHttpLoading() {
        if (httpDialog != null && httpDialog!!.isShowing) {
            httpDialog!!.dismiss()
        }
    }

    override fun onDestroy() {
        hideHttpLoading()
        httpDialog = null
        if (this::class.java.isAnnotationPresent(BindEventBus::class.java) &&
            EventBus.getDefault().isRegistered(this)
        ) {
            EventBus.getDefault().unregister(this)
        }
        super.onDestroy()
        AppWatcher.objectWatcher.watch(this)
    }


}